/* ----------------------------------------------------------------------
*
* Copyright (C) 2025 XiaoFei. All rights reserved.
*
* $file:		XiaoZhi_VL53L0X.h
* $Date:        01-March-2025
* $Revision: 	V1
* $Project: 	XiaoFei BSP Library
* $Taobao:      https://item.taobao.com/item.htm?abbucket=17&id=806904896474
*
* -------------------------------------------------------------------- */

#ifndef __XiaoZhi_VL53L0X_H
#define __XiaoZhi_VL53L0X_H

#define VL53L0X_SDA_RCC(x)			    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,(FunctionalState)x)
#define VL53L0X_GPIO_SDA				GPIOA
#define VL53L0X_PIN_SDA					GPIO_Pin_0
#define VL53L0X_SCL_RCC(x)			    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,(FunctionalState)x)
#define VL53L0X_GPIO_SCL				GPIOA
#define VL53L0X_PIN_SCL					GPIO_Pin_1
#define VL53L0X_XSH_RCC(x)			    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,(FunctionalState)x)
#define VL53L0X_GPIO_XSH				GPIOA
#define VL53L0X_PIN_XSH					GPIO_Pin_2
#define	VL53L0X_XSH(x)					GPIO_WriteBit(VL53L0X_GPIO_XSH,VL53L0X_PIN_XSH,(BitAction)x)
#define VL53L0X_SCL(x)					do{GPIO_WriteBit(VL53L0X_GPIO_SCL,VL53L0X_PIN_SCL,(BitAction)(x)),XiaoFei_Delay_us(2);}while(0)
#define VL53L0X_SDA(x)					do{GPIO_WriteBit(VL53L0X_GPIO_SDA,VL53L0X_PIN_SDA,(BitAction)(x)),XiaoFei_Delay_us(2);}while(0)
#define VL53L0X_SDARead					GPIO_ReadInputDataBit(VL53L0X_GPIO_SDA,VL53L0X_PIN_SDA)
#define VL53L0X_Address					0x52

#ifndef VL53L0X_Sche_def
#define VL53L0X_Sche_def
typedef struct{
	unsigned char TccOn;	    /*!< Reports if Target Centre Check On */
	unsigned char MsrcOn;	    /*!< Reports if MSRC On */
	unsigned char DssOn;	    /*!< Reports if DSS On */
	unsigned char PreRangeOn;   /*!< Reports if Pre-Range On */
	unsigned char FinalRangeOn; /*!< Reports if Final-Range On */
}VL53L0X_Sche;
#endif

#ifndef VL53L0X_TypeDef_def
#define VL53L0X_TypeDef_def
typedef struct{
	unsigned char* SpadEnables; /*!< Reference Spad Enables */
	unsigned char* SpadMap;     /*!< Reference Spad Good Spad Map */
	unsigned short int Rate;    /*!< Target Ambient Rate for Ref spad management */
	unsigned char Sequence;     /*!< Internal value for the sequence config */
	unsigned short int Range;   /*!< Ranging Data from device */
}VL53L0X_TypeDef;
#endif

#ifndef VL53L0X_MoodDef_def
#define VL53L0X_MoodDef_def
typedef enum{
	VL53L0X_FastMood = 33000,   /*!< Set VL53L0X to quick measurement mode */
	VL53L0X_AccuMood = 20000    /*!< Set VL53L0X to high-precision measurement mode */
}VL53L0X_MoodDef;
#endif

#ifndef VL53L0X_Error_def
#define VL53L0X_Error_def
typedef enum{
	VL53L0X_ErrNone = 0x00,     /*!< No errors occur */
	VL53L0X_InvPara = 0x01,     /*!< Parameter passed is invalid or out of range */
	VL53L0X_InterCl = 0x02,     /*!< Parameter passed is invalid or out of range */
	VL53L0X_TimeOut = 0x03,     /*!< Aborted due to time out */
	VL53L0X_SpadInt = 0x04,     /*!< Error during reference SPAD initialization */
	VL53L0X_RangOut = 0x05      /*!< Measurement result is not within the effective distance */
}VL53L0X_Error;
#endif

/* 
* @function: 	VL53L0X_Init
* @parameter:	[out] Dev				    The Device handle of VL53L0X.
*/
void VL53L0X_Init(VL53L0X_TypeDef* Dev);

/* 
* @function: 	VL53L0X_SetMood
* @parameter:	[in] Dev				    The Device handle of VL53L0X.
* @parameter:	[in] Mood				    This parameter can be any value of @ref VL53L0X_MoodDef.
* @return:		Return VL53L0X_ErrNone if the mood is set successful.
*/
VL53L0X_Error VL53L0X_SetMood(VL53L0X_TypeDef* Dev, VL53L0X_MoodDef Mood);

/* 
* @function: 	VL53L0X_GetData
* @parameter:	[in] Dev				    The Device handle of VL53L0X.
* @parameter:	[out] buf				    The distance obtained is measured.
* @return:		Return VL53L0X_ErrNone if the measure is set successful.
*/
VL53L0X_Error VL53L0X_GetData(VL53L0X_TypeDef* Dev,unsigned int *buf);

/* 
* @function: 	VL53L0X_Free
* @parameter:	[in] Dev				    The Device handle of VL53L0X will be free.
*/
void VL53L0X_Free(VL53L0X_TypeDef* Dev);

#endif



